package com.bsy.framework.manager

import android.os.Parcelable
import com.bsy.framework.ext.toJson
import com.google.gson.Gson
import com.tencent.mmkv.MMKV
import java.lang.reflect.Type


/**
 * Created by Sincerity on 2022/7/22.
 * Copyright
 * 描述：MMKV
 */
object KvManager {
    private var instance: MMKV? = null

    init {
        instance = MMKV.defaultMMKV()
    }

    /**
     * 保存数据的方法，我们需要拿到保存数据的具体类型，然后根据类型调用不同的保存方法
     *
     * @param key
     * @param object
     */
    fun save(key: String?, any: Any): Boolean? {
        return when (any) {
            is String -> {
                instance?.encode(key, any)
            }

            is Int -> {
                instance?.encode(key, any)
            }

            is Boolean -> {
                instance?.encode(key, any)
            }

            is Float -> {
                instance?.encode(key, any)
            }

            is Long -> {
                instance?.encode(key, any)
            }

            is Double -> {
                instance?.encode(key, any)
            }

            is ByteArray -> {
                instance?.encode(key, any)
            }

            else -> {
                instance?.encode(key, any.toString())
            }
        }
    }

    fun saveList(key: String?, collection: Collection<*>) {
        instance?.encode(key, collection.toJson())
    }

    fun <T> getList(key: String?, type: Type): T? {
        val value = instance?.decodeString(key)
        return if (value != null)
            Gson().fromJson(value, type)
        else null
    }

    fun saveSet(key: String?, sets: Set<String?>?) {
        instance?.encode(key, sets)
    }

    fun saveParcelable(key: String?, obj: Parcelable?) {
        instance?.encode(key, obj)
    }

    /**
     * 得到保存数据的方法，我们根据默认值得到保存的数据的具体类型，然后调用相对于的方法获取值
     */
    fun getInt(key: String?, defaultValue: Int = 0): Int {
        return instance?.decodeInt(key, defaultValue) ?: defaultValue
    }

    fun getDouble(key: String?, defaultValue: Double = 0.0): Double {
        return instance?.decodeDouble(key, 0.00) ?: defaultValue
    }

    fun getLong(key: String?, defaultValue: Long = 0L): Long {
        return instance?.decodeLong(key, defaultValue) ?: defaultValue
    }

    fun getBoolean(key: String?, defaultValue: Boolean = false): Boolean {
        return instance?.decodeBool(key, defaultValue) ?: defaultValue
    }

    fun getFloat(key: String?, defaultValue: Float = 0F): Float {
        return instance?.decodeFloat(key, defaultValue) ?: defaultValue
    }

    fun getBytes(key: String?, defaultValue: ByteArray = ByteArray(0)): ByteArray {
        return instance?.decodeBytes(key) ?: defaultValue
    }

    fun getString(key: String?, defaultValue: String = ""): String {
        return instance?.decodeString(key, defaultValue) ?: defaultValue
    }


    fun getSet(key: String?, defaultValue: Set<String> = emptySet()): Set<String>? {
        return instance?.decodeStringSet(key, defaultValue)
    }

    fun <T : Parcelable?> getParcelable(key: String?, tClass: Class<T>?): T? {
        return instance?.decodeParcelable(key, tClass)
    }

    /**
     * 移除某个key对
     *
     * @param key
     */
    fun removeKey(key: String) {
        instance?.removeValueForKey(key)
    }

    fun contains(key: String): Boolean =
        instance?.contains(key) ?: false


    /**
     * 清除所有key
     */
    fun clearAll() {
        instance?.clearAll()
    }
}